package com.itheima.d2_ThreadPool_Callable;

import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;

/**
 *  public ThreadPoolExecutor(int corePoolSize,
 *                               int maximumPoolSize,
 *                               long keepAliveTime,
 *                               TimeUnit unit,
 *                               BlockingQueue<Runnable> workQueue,
 *                               ThreadFactory threadFactory,
 *                               RejectedExecutionHandler handler)
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        //创建线程池
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8
                , TimeUnit.SECONDS, new ArrayBlockingQueue<>(5)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy());
        // 给任务
        Callable<String> myCallable = new MyCallable();
        //仍到线程池, 注意 runnable是execute, callable是submit 这个可以直接仍mycallable
        Future<String> f1 = pool.submit(myCallable);
        Future<String> f2 = pool.submit(myCallable);
        Future<String> f3 = pool.submit(myCallable);
        //这个不太可能创建临时线程
        Future<String> f4 = pool.submit(myCallable);
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
